/*
 * @(#)WorkflowScheduleTriggers.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.bean;

import java.sql.Timestamp;

import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;

/**
 WorkflowScheduleTriggers.java
 
 This class is the serializable bean reflecting business logic uses.
 
 @author           Tony Chen
 @company          DCIVision Limited
 @creation date    22/12/2004
 @version          $Revision: 1.7 $
 */

public class WorkflowScheduleTriggers extends AbstractBaseObject {
  
  public static final String REVISION = "$Revision: 1.7 $";
  static final long serialVersionUID = 2100526990363965888L;
  
  private Integer workflowRecordID = null;
  private String scheduleName = null;
  private String criteriaExpression = null;
  private String criteriaConfig = null;
  private Timestamp scheduleStart = null;
  private Timestamp scheduleEnd = null;
  private Integer startTime = null;
  private Integer scheduleType = null;
  private Integer scheduleStyle = null;
  private String includeList = null;
  private String extraParameter1 = null;
  private String extraParameter2 = null;
  private Integer recurrRunCnt = null;
  private Integer recurrMaxRun = null;
  private String recurrExpression = null;
  private String status = null;
  
  private String workflowCode = null;
  private String schedule = null;
  private Timestamp previousFireTime = null;
  private Timestamp nextFireTime = null;
  private String lastResult = null;
  
  public WorkflowScheduleTriggers() {
    super();
  }
  /**
   * @return Returns the workflowRecordID.
   */
  public Integer getWorkflowRecordID() {
    return(this.workflowRecordID);
  }
  /**
   * @param workflowRecordID The workflowRecordID to set.
   */
  public void setWorkflowRecordID(Integer workflowRecordID) {
    this.workflowRecordID = workflowRecordID;
  }
  /**
   * @return Returns the scheduleName.
   */
  public String getScheduleName() {
    return(this.scheduleName);
  }
  /**
   * @param scheduleName The scheduleName to set.
   */
  public void setScheduleName(String scheduleName) {
    this.scheduleName = scheduleName;
  }
  /**
   * @return Returns the criteriaExpression.
   */
  public String getCriteriaExpression() {
    return(this.criteriaExpression);
  }
  /**
   * @param criteriaExpression The criteriaExpression to set.
   */
  public void setCriteriaExpression(String criteriaExpression) {
    this.criteriaExpression = criteriaExpression;
  }
  /**
   * @return Returns the criteriaConfig.
   */
  public String getCriteriaConfig() {
    return(this.criteriaConfig);
  }
  /**
   * @param criteriaConfig The criteriaConfig to set.
   */
  public void setCriteriaConfig(String criteriaConfig) {
    this.criteriaConfig = criteriaConfig;
  }
  /**
   * @return Returns the scheduleStart.
   */
  public Timestamp getScheduleStart() {
    return(this.scheduleStart);
  }
  /**
   * @param scheduleStart The scheduleStart to set.
   */
  public void setScheduleStart(Timestamp scheduleStart) {
    this.scheduleStart = scheduleStart;
  }
  /**
   * @return Returns the scheduleEnd.
   */
  public Timestamp getScheduleEnd() {
    return(this.scheduleEnd);
  }
  /**
   * @param scheduleEnd The scheduleEnd to set.
   */
  public void setScheduleEnd(Timestamp scheduleEnd) {
    this.scheduleEnd = scheduleEnd;
  }
  /**
   * @return Returns the startTime.
   */
  public Integer getStartTime() {
    return startTime;
  }
  /**
   * @param startTime The startTime to set.
   */
  public void setStartTime(Integer startTime) {
    this.startTime = startTime;
  }
  /**
   * @return Returns the scheduleType.
   */
  public Integer getScheduleType() {
    return(this.scheduleType);
  }
  /**
   * @param scheduleType The scheduleType to set.
   */
  public void setScheduleType(Integer scheduleType) {
    this.scheduleType = scheduleType;
  }
  /**
   * @return Returns the scheduleStyle.
   */
  public Integer getScheduleStyle() {
    return(this.scheduleStyle);
  }
  /**
   * @param scheduleStyle The scheduleStyle to set.
   */
  public void setScheduleStyle(Integer scheduleStyle) {
    this.scheduleStyle = scheduleStyle;
  }
  /**
   * @return Returns the includeList.
   */
  public String getIncludeList() {
    return(this.includeList);
  }
  /**
   * @param includeList The includeList to set.
   */
  public void setIncludeList(String includeList) {
    this.includeList = includeList;
  }
  /**
   * @return Returns the extraParameter1.
   */
  public String getExtraParameter1() {
    return(this.extraParameter1);
  }
  /**
   * @param extraParameter1 The extraParameter1 to set.
   */
  public void setExtraParameter1(String extraParameter1) {
    this.extraParameter1 = extraParameter1;
  }
  /**
   * @return Returns the extraParameter2.
   */
  public String getExtraParameter2() {
    return(this.extraParameter2);
  }
  /**
   * @param extraParameter2 The extraParameter2 to set.
   */
  public void setExtraParameter2(String extraParameter2) {
    this.extraParameter2 = extraParameter2;
  }
  /**
   * @return Returns the recurrRunCnt.
   */
  public Integer getRecurrRunCnt() {
    return(this.recurrRunCnt);
  }
  /**
   * @param recurrRunCnt The recurrRunCnt to set.
   */
  public void setRecurrRunCnt(Integer recurrRunCnt) {
    this.recurrRunCnt = recurrRunCnt;
  }
  /**
   * @return Returns the recurrMaxRun.
   */
  public Integer getRecurrMaxRun() {
    return(this.recurrMaxRun);
  }
  /**
   * @param recurrMaxRun The recurrMaxRun to set.
   */
  public void setRecurrMaxRun(Integer recurrMaxRun) {
    this.recurrMaxRun = recurrMaxRun;
  }
  /**
   * @return Returns the recurrExpression.
   */
  public String getRecurrExpression() {
    return(this.recurrExpression);
  }
  /**
   * @param recurrExpression The recurrExpression to set.
   */
  public void setRecurrExpression(String recurrExpression) {
    this.recurrExpression = recurrExpression;
  }
  /**
   * @return Returns the status.
   */
  public String getStatus() {
    return status;
  }
  /**
   * @param status The status to set.
   */
  public void setStatus(String status) {
    this.status = status;
  }
  /**
   * @return Returns the previousFireTime.
   */
  public Timestamp getPreviousFireTime() {
    return previousFireTime;
  }
  /**
   * @param previousFireTime The previousFireTime to set.
   */
  public void setPreviousFireTime(Timestamp previousFireTime) {
    this.previousFireTime = previousFireTime;
  }
  /**
   * @return Returns the lastResult.
   */
  public String getLastResult() {
    return lastResult;
  }
  /**
   * @param lastResult The lastResult to set.
   */
  public void setLastResult(String lastResult) {
    this.lastResult = lastResult;
  }
  /**
   * @return Returns the nextFireTime.
   */
  public Timestamp getNextFireTime() {
    return nextFireTime;
  }
  /**
   * @param nextFireTime The nextFireTime to set.
   */
  public void setNextFireTime(Timestamp nextFireTime) {
    this.nextFireTime = nextFireTime;
  }
  /**
   * @return Returns the schedule.
   */
  public String getSchedule() {
    return schedule;
  }
  /**
   * @param schedule The schedule to set.
   */
  public void setSchedule(String schedule) {
    this.schedule = schedule;
  }
  /**
   * @return Returns the workflowCode.
   */
  public String getWorkflowCode() {
    return workflowCode;
  }
  /**
   * @param workflowCode The workflowCode to set.
   */
  public void setWorkflowCode(String workflowCode) {
    this.workflowCode = workflowCode;
  }
  
  /**
   * get WorkflowSchedule by WorkflowScheduleTriggers
   * @param scheduleTrigger
   * @return
   */
  public WorkflowSchedule getWorkflowSchedule() {
    WorkflowSchedule schedule = new WorkflowSchedule();
    
    schedule.setID(this.getID());
    schedule.setTriggerName(this.getScheduleName());
    schedule.setTriggerType(this.getScheduleType().intValue());
    
    if ( !Utility.isEmpty(this.getScheduleStyle()) ) {
      schedule.setTriggerStyle(this.getScheduleStyle().intValue());
    }
    if ( !Utility.isEmpty(this.getRecurrMaxRun()) ) {
      schedule.setMaxRun(this.getRecurrMaxRun().intValue());
    }
    
    if ( !Utility.isEmpty(this.getRecurrRunCnt()) ){
      schedule.setCurrentRun(this.getRecurrRunCnt().intValue());
    }
    schedule.setStartDate(this.getScheduleStart());
    schedule.setEndDate(this.getScheduleEnd());
    
    if ( !Utility.isEmpty(this.getStartTime()) ) {
      int triggerHour = (int) (this.getStartTime().intValue() / (60 * 60 * 1000) );
      int triggerMinute = (int) ( (this.getStartTime().intValue() - triggerHour * 60 * 60 * 1000) / (60 * 1000) );
      schedule.setTriggerHour(triggerHour);
      schedule.setTriggerMinute(triggerMinute);
    }
    
    if ( !Utility.isEmpty(this.getExtraParameter1()) &&
        (WorkflowSchedule.TRIGGER_TYPE_DAILY == this.getScheduleType().intValue() ||
            WorkflowSchedule.TRIGGER_TYPE_WEEKLY == this.getScheduleType().intValue())
    ) {
      schedule.setInterval( Integer.parseInt(this.getExtraParameter1()) );
    }
    
    if ( !Utility.isEmpty(this.getIncludeList()) ) {
      if ( WorkflowSchedule.TRIGGER_TYPE_WEEKLY == this.getScheduleType().intValue() ) {
        schedule.setDaysOfWeek(TextUtility.splitString(this.getIncludeList(), WorkflowSchedule.EXPR_SPECIAL_ADDITIONAL));
      } else if ( WorkflowSchedule.TRIGGER_TYPE_MONTHLY == this.getScheduleType().intValue() ) {
        schedule.setMonthsOfYear(TextUtility.splitString(this.getIncludeList(), WorkflowSchedule.EXPR_SPECIAL_ADDITIONAL));
      }
    }
    
    // Monthly: day of month
    if ( !Utility.isEmpty(this.getScheduleStyle()) && WorkflowSchedule.MONHTLY_STYLE_DAY_OF_MONTH == this.getScheduleStyle().intValue() ) {
      schedule.setDayOfMonth(this.getExtraParameter1());
      // Monthly: week of month
    } else if ( !Utility.isEmpty(this.getScheduleStyle()) && WorkflowSchedule.MONHTLY_STYLE_WEEK_OF_MONTH == this.getScheduleStyle().intValue() ) {
      schedule.setWeekOfMonth(this.getExtraParameter1());
      schedule.setDayOfWeek(this.getExtraParameter2());
    }
    
    return schedule;
  }
  
}
